
package jugueteria_lm;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Apartado extends javax.swing.JFrame {

      Jugueteria_Datos obj = new Jugueteria_Datos();
    Conexion conectar = new Conexion();
    
    public Apartado() {
        initComponents();
        MostrarApartado("");
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Tapartado = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        apar1 = new javax.swing.JTextField();
        apart2 = new javax.swing.JTextField();
        apart3 = new javax.swing.JTextField();
        apart4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        apart5 = new javax.swing.JTextField();
        apart6 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        buscaapart = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaVista = new javax.swing.JTable();
        jComboBox1 = new javax.swing.JComboBox();
        jButton8 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        Tapartado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(Tapartado);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 130, 529, 118));

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 60, -1, -1));

        jButton2.setText("Actualizar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 30, -1, -1));

        jButton3.setText("Nuevo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 30, -1, -1));

        jLabel1.setText("ID");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 32, -1));

        jLabel2.setText("Precio Total");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(69, 27, -1, -1));

        jLabel3.setText("Abono");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(143, 27, -1, -1));

        jLabel4.setText("Cantidad Faltante");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(211, 27, -1, -1));
        getContentPane().add(apar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 41, -1));

        apart2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                apart2ActionPerformed(evt);
            }
        });
        getContentPane().add(apart2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 50, 56, -1));
        getContentPane().add(apart3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 50, 48, -1));
        getContentPane().add(apart4, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 50, 54, -1));

        jLabel5.setText("Id cliente");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(315, 27, -1, -1));

        jLabel6.setText("Id juguete");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 30, -1, -1));
        getContentPane().add(apart5, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 50, 52, -1));
        getContentPane().add(apart6, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 50, 56, -1));

        jButton4.setText("Modificar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 60, -1, -1));

        jButton5.setText("Mostrar Apartado");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 100, -1, -1));

        jButton6.setText("Eliminar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 90, -1, -1));

        jButton7.setText("Buscar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 100, -1, -1));
        getContentPane().add(buscaapart, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 100, 60, -1));

        TablaVista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(TablaVista);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 300, 529, 118));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SelecioneTabla", "Clientes", "Juguetes", " " }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 270, -1, -1));

        jButton8.setBackground(new java.awt.Color(255, 51, 51));
        jButton8.setText("Salir");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(488, 433, 90, 30));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jugueteria_lm/4.jpg"))); // NOI18N
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 600, 470));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here://agregar apartado
      try {
            try (PreparedStatement insertar = conectar.getConnection().prepareStatement("insert into apartado(idapartado,precio_total ,abono,cantidad_faltante,idcliente,id_juguete) "
                            + " values(?,?,?,?,?,?)")) {
                insertar.setString(1, apar1.getText());
                insertar.setString(2, apart2.getText());
                insertar.setString(3, apart3.getText());
                insertar.setString(4, apart4.getText());
                insertar.setString(5, apart5.getText());
                insertar.setString(6, apart6.getText());
                insertar.execute();
                insertar.close();
                JOptionPane.showMessageDialog(null, "Apartado Agregado");
                MostrarApartado("");
            }
        } catch (SQLException e) {
            //JOptionPane.showMessageDialog(null, "ID (" + apar1.getText() + ") en uso..");
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void apart2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_apart2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_apart2ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here://actualizar apartado
         try {

            PreparedStatement pre = conectar.getConnection().prepareStatement("UPDATE apartado "
                    + "SET precio_total='" + apart2.getText() + "', abono='" + apart3.getText() + "', cantidad_faltante='" + apart4.getText()+
                   "',idcliente='" + apart5.getText()+  "',id_juguete='" + apart6.getText()+ "' WHERE idapartado='" + apar1.getText() + "';");
            pre.executeUpdate();

            JOptionPane.showMessageDialog(this, "Se Actualizo con exito");
            MostrarApartado("");
        } catch (SQLException ex) {
            //JOptionPane.showMessageDialog(this, "Error al Corregir");
            System.out.print(ex.getMessage());
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here://apartado
           // TODO add your handling code here:
        apar1.setText(null);
        apart2.setText(null);
        apart3.setText(null);
        apart4.setText(null);
        apart5.setText(null);
        apart6.setText(null);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here://apartado
         int fila2 = Tapartado.getSelectedRow();
        if (fila2 >= 0) {
            apar1.setText(Tapartado.getValueAt(fila2, 0).toString());
            apart2.setText(Tapartado.getValueAt(fila2, 1).toString());
            apart3.setText(Tapartado.getValueAt(fila2, 2).toString());
            apart4.setText(Tapartado.getValueAt(fila2, 3).toString());
            apart5.setText(Tapartado.getValueAt(fila2, 4).toString());
            apart6.setText(Tapartado.getValueAt(fila2, 5).toString());

        } else {
            JOptionPane.showMessageDialog(null, "Seleccionado una fila por favor");

        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here://apartado
         DefaultTableModel ModeloApartado = new DefaultTableModel();

        ModeloApartado.addColumn("ID Apartado");
        ModeloApartado.addColumn("Precio Total");
        ModeloApartado.addColumn("Abono");
        ModeloApartado.addColumn("cantidad Faltante");
        ModeloApartado.addColumn("ID Cliente");
        ModeloApartado.addColumn("ID Juguete");

        

        Tapartado.setModel(ModeloApartado);

        String[] datosapart = new String[6];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM apartado");
            while (rs.next()) {
                 datosapart[0] = rs.getString(1);
                 datosapart[1] = rs.getString(2);
                 datosapart[2] = rs.getString(3);
                 datosapart[3] = rs.getString(4);
                 datosapart[4] = rs.getString(5);
                 datosapart[5] = rs.getString(6);


                ModeloApartado.addRow( datosapart);

            }
            Tapartado.setModel(ModeloApartado);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here://apartado
          int fild = Tapartado.getSelectedRow();
        if (fild > -1) {
            String id = String.valueOf(Tapartado.getValueAt(fild, 0));
            obj.Elimina_Datos("apartado", "idapartado", id);
            MostrarApartado("");
            fild = -1;
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here://apartado
        MostrarApartado(buscaapart.getText());
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
       String Tabla= jComboBox1.getSelectedItem().toString();
       String Seleccion= "SeleccioneTabla";
       if (Tabla != Seleccion){
       if(Tabla.equals("Clientes")){
           
           MostrarCliente("");
           }else if(Tabla.equals("Juguetes")){
           MostrarJuguete("");
           }
       }
        // TODO add your handling code here:
        /*
Clientes
Juguetes*/
        
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton8ActionPerformed

    void MostrarCliente(String muestra) {
        DefaultTableModel ModeloCliente = new DefaultTableModel();

        ModeloCliente.addColumn("ID Cliente");
        ModeloCliente.addColumn(" Nombre");
        ModeloCliente.addColumn("Direccion");
        ModeloCliente.addColumn("Telefono");

        TablaVista.setModel(ModeloCliente);
        String sql = "";
        if (muestra.equals("")) {

            sql = "SELECT * FROM cliente";
        } else {
            sql = "SELECT * FROM cliente WHERE idcliente ='" + muestra + "'";
        }
        String[] datosCliente = new String[4];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                datosCliente[0] = rs.getString(1);
                datosCliente[1] = rs.getString(2);
                datosCliente[2] = rs.getString(3);
                datosCliente[3] = rs.getString(4);

                ModeloCliente.addRow(datosCliente);

            }
            TablaVista.setModel(ModeloCliente);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
    
    void MostrarJuguete(String juguete) {
        DefaultTableModel ModeloJuguete = new DefaultTableModel();

        ModeloJuguete.addColumn("ID juguete");
        ModeloJuguete.addColumn("Nombre ");
        ModeloJuguete.addColumn("Marca");
        ModeloJuguete.addColumn("Precio unitario");
        ModeloJuguete.addColumn("Area");
        ModeloJuguete.addColumn("cantidad de piezas");
        ModeloJuguete.addColumn("Proveedor");

        TablaVista.setModel(ModeloJuguete);
        String sql = "";
        if (juguete.equals("")) {

            sql = "SELECT * FROM juguete";
        } else {
            sql = "SELECT * FROM juguete WHERE id_juguete ='" + juguete + "'";
        }
        String[] datosClientess = new String[7];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                datosClientess[0] = rs.getString(1);
                datosClientess[1] = rs.getString(2);
                datosClientess[2] = rs.getString(3);
                 datosClientess[3] = rs.getString(4);
                  datosClientess[4] = rs.getString(5);
                    datosClientess[5] = rs.getString(6);
                datosClientess[6] = rs.getString(7);


                ModeloJuguete.addRow(datosClientess);

            }
            TablaVista.setModel(ModeloJuguete);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * @param args the command line arguments
     */
    
      void MostrarApartado(String muestraapar) {
        DefaultTableModel ModeloApartado = new DefaultTableModel();

        ModeloApartado.addColumn("ID Apartado");
        ModeloApartado.addColumn(" Precio Total");
        ModeloApartado.addColumn("Abono");
        ModeloApartado.addColumn("Cantida Faltante");
       ModeloApartado.addColumn("ID Cliente");
       ModeloApartado.addColumn("ID Juguete");


        Tapartado.setModel(ModeloApartado);
        String sql = "";
        if (muestraapar.equals("")) {

            sql = "SELECT * FROM apartado";
        } else {
            sql = "SELECT * FROM apartado WHERE idapartado ='" + muestraapar + "'";
        }
        String[] datosclientes = new String[6];
        try {
            Statement st = conectar.getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                datosclientes[0] = rs.getString(1);
                datosclientes[1] = rs.getString(2);
                datosclientes[2] = rs.getString(3);
                datosclientes[3] = rs.getString(4);
                datosclientes[4] = rs.getString(5); 
                datosclientes[5] = rs.getString(6);

                ModeloApartado.addRow(datosclientes);

            }
            Tapartado.setModel(ModeloApartado);
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
      
      

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Apartado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Apartado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Apartado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Apartado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Apartado().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablaVista;
    private javax.swing.JTable Tapartado;
    private javax.swing.JTextField apar1;
    private javax.swing.JTextField apart2;
    private javax.swing.JTextField apart3;
    private javax.swing.JTextField apart4;
    private javax.swing.JTextField apart5;
    private javax.swing.JTextField apart6;
    private javax.swing.JTextField buscaapart;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
